package com.zhugang.week04;

/**
 * @program algorithms
 * @description: reversePairs
 * @author: chanzhugang
 * @create: 2022/07/01 08:03
 */
public class ReversePairs {

    public static void main(String[] args) {
        ReversePairs reversePairs = new ReversePairs();
        int[] nums = new int[]{7, 5, 6, 4};
        int count = reversePairs.reversePairs(nums);
        System.out.println(count);
    }

    /**
     * 剑指offer 51 数组中的逆序对
     *
     * @param nums
     * @return
     */
    public int reversePairs(int[] nums) {
        // 暴力求解
        int count = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[i]) {
                    // 前面的比后面的数大
                    count++;
                }
            }
        }
        return count;
    }
}